//
//  UIButton++.swift
//  iOS_Com_Extensions
//
//  Created by 家和20240415 on 2024/7/11.
//

import UIKit

//MARK: -定义button中image的位置
@objc public enum ButtonEdgeInsetsStyle: Int {
    case top
    case left
    case right
    case bottom
}

public extension UIButton {
    var titleWidth: CGFloat {
        if let title = title(for: .normal), let font = titleLabel?.font {
            let labelWidth = title.stringWidth(font: font)
            debugPrint("labelWidth  -- \(labelWidth), \(title)")
            return labelWidth
        }
        if #available(iOS 8.0, *) {
            return self.titleLabel?.intrinsicContentSize.width ?? 0
        } else {
            return self.titleLabel?.frame.size.width ?? 0
        }
    }
    @objc func layoutButton(style: ButtonEdgeInsetsStyle, imageTitleSpace: CGFloat) {
        layoutButton(style: style, imageTitleSpace: imageTitleSpace, shouldFold: true)
    }
    @objc func layoutButton(style: ButtonEdgeInsetsStyle, imageTitleSpace: CGFloat, shouldFold: Bool = true) {
        //得到imageView和titleLabel的宽高
        let imageWidth = self.imageView?.frame.size.width ?? 0
        let imageHeight = self.imageView?.frame.size.height ?? 0
        
        var labelWidth: CGFloat! = titleWidth
        var labelHeight: CGFloat! = 0.0
        
        if #available(iOS 8.0, *) {
//            labelWidth = self.titleLabel?.intrinsicContentSize.width
            labelHeight = self.titleLabel?.intrinsicContentSize.height
        } else {
//            labelWidth = self.titleLabel?.frame.size.width
            labelHeight = self.titleLabel?.frame.size.height
        }
        
        if (labelWidth + imageWidth + imageTitleSpace) > frame.width && shouldFold == true{
            labelWidth = frame.width - imageWidth - imageTitleSpace/2
        }
        
        //根据style和space得到imageEdgeInsets和labelEdgeInsets的值
        switch style {
        case .left:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: -imageTitleSpace/2, bottom: 0, right:imageTitleSpace/2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: imageTitleSpace/2, bottom: 0, right: -imageTitleSpace/2)
            break
        case .top:
            imageEdgeInsets = UIEdgeInsets(top: -labelHeight-imageTitleSpace/2, left: (frame.width - imageWidth)/2, bottom: 0, right: 0)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth, bottom: -imageHeight-imageTitleSpace/2, right: 0)
            break
        case .right:
            
            imageEdgeInsets = UIEdgeInsets(top: 0, left: labelWidth+imageTitleSpace/2, bottom: 0, right: -labelWidth-imageTitleSpace/2)
            titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageWidth-imageTitleSpace/2, bottom: 0, right: imageWidth+imageTitleSpace/2)
            break
        case .bottom:
            imageEdgeInsets = UIEdgeInsets(top: 0, left: (frame.width - imageWidth)/2, bottom: -labelHeight-imageTitleSpace/2, right: labelWidth)
            titleEdgeInsets = UIEdgeInsets(top: -imageHeight-imageTitleSpace/2, left: -imageWidth, bottom: 0, right: 0)
            break
        }
    }
}
